package edu.ruc.cluster.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;

import edu.ruc.cluster.print.ConsolePane;

@SuppressWarnings("serial")
public class DefaultLogHandler extends JFrame implements LogHandler {
	
	public static DefaultLogHandler logHandler = null;
	public static synchronized DefaultLogHandler getInstance(){
		if(logHandler == null){
			logHandler = new DefaultLogHandler();
		}
		return logHandler;
	}
	private DefaultLogHandler(){
		setTitle("Redirect System.out");
		setSize(640, 240);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
		getContentPane().add(ConsolePane.getInstance());
//		JButton button = new JButton("Click Me to Output Message");
//		getContentPane().add(button, BorderLayout.SOUTH);
//		
//		button.addActionListener(new ActionListener(){
//			public void actionPerformed(ActionEvent e){
//				Random random = new Random();
//				int num = random.nextInt(10);
//				String msg = ": Hello Yatou, Redirect"
//						+ ((num %2 == 1) ? "\"System.out\"" : "\"System.err\"")
//						+ " to ConsolePane, today:";
//				if(num % 2 == 1)
//					System.out.println(num + msg + new Date());
//				else
//					System.err.println(num + msg + new Date());
//			}
//		});
		setVisible(true);
	}
	private static final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");  
	
	private void log(String msg){  
		        System.out.println(sdf.format(new Date())+ " " + msg);  
		    }  
    private void log(String msg, Throwable t){  
        synchronized (System.out) {  
            System.out.println(sdf.format(new Date())+ " " + msg);  
            t.printStackTrace(System.out);  
        }  
    }  

	@Override
	public void debug(String msg) {
		// TODO Auto-generated method stub
		log(msg);
	}

	@Override
	public void debug(String msg, Throwable t) {
		// TODO Auto-generated method stub
		log(msg, t);
	}

	@Override
	public void error(String msg) {
		// TODO Auto-generated method stub
		System.err.println(sdf.format(new Date()) + " " + msg);
	}

	@Override
	public void error(String msg, Throwable t) {
		// TODO Auto-generated method stub
		synchronized(System.err){
			System.err.println(sdf.format(new Date())+ " " + msg);  
			t.printStackTrace(System.err);  

		}
	}

	@Override
	public void info(String msg) {
		// TODO Auto-generated method stub
		log(msg);
	}

	@Override
	public void info(String msg, Throwable t) {
		// TODO Auto-generated method stub
		log(msg, t);
	}
	/**
	private static final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");  
	
	private void log(String msg){  
		        System.out.println(sdf.format(new Date())+ " " + msg);  
		    }  
    private void log(String msg, Throwable t){  
        synchronized (System.out) {  
            System.out.println(sdf.format(new Date())+ " " + msg);  
            t.printStackTrace(System.out);  
        }  
    }  

	@Override
	public void debug(String msg) {
		// TODO Auto-generated method stub
		log(msg);
	}

	@Override
	public void debug(String msg, Throwable t) {
		// TODO Auto-generated method stub
		log(msg, t);
	}

	@Override
	public void error(String msg) {
		// TODO Auto-generated method stub
		System.err.println(sdf.format(new Date()) + " " + msg);
	}

	@Override
	public void error(String msg, Throwable t) {
		// TODO Auto-generated method stub
		synchronized(System.err){
			System.err.println(sdf.format(new Date())+ " " + msg);  
			t.printStackTrace(System.err);  

		}
	}

	@Override
	public void info(String msg) {
		// TODO Auto-generated method stub
		log(msg);
	}

	@Override
	public void info(String msg, Throwable t) {
		// TODO Auto-generated method stub
		log(msg, t);
	}
	*/
}
